package edu.zcmu.seckill.controller;


import edu.zcmu.seckill.response.R;
import edu.zcmu.seckill.service.UserService;
import edu.zcmu.seckill.vo.AlterPasswordVo;
import edu.zcmu.seckill.vo.LoginVo;
import edu.zcmu.seckill.vo.PayVo;
import edu.zcmu.seckill.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 唐诗
 * @since 2022-02-17
 */
@Slf4j
@RestController
@RequestMapping("/seckill/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        log.info("用户登录{}",loginVo.getIdcard());
       return userService.login(loginVo);
    }

    @PostMapping("register")
    public R register(@RequestBody @Valid RegisterVo registerVo, BindingResult result){
        if (result.hasErrors()){
            return R.error().message(result.getFieldError().getDefaultMessage());
        }
        return userService.register(registerVo);
    }

    /**
     * 得到用户所有订单
     * @param request
     * @return
     */
    @GetMapping("/getAllOrder")
    public R getAllOrder(@RequestParam("pageNum") Integer pageNum,HttpServletRequest request){
        HashMap<String, Object> list=userService.getAllOrder(pageNum,request);

        return R.ok().data(list);
    }

    @PostMapping("/delUserOrder")
    public R delUserOrder(@RequestBody PayVo payVo,HttpServletRequest request){
        R r=userService.delUserOrder(payVo,request);
        return r;
    }

    @PostMapping("/alterPassword")
    public R alterPassword(@RequestBody AlterPasswordVo passwordVo,HttpServletRequest request){
        R r=userService.alterPassword(passwordVo,request);
        return r;
    }

    @PostMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        R r=userService.getUserInfo(request);
        return r;
    }
}
